package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RequestMapping("/admin/product")
@RestController
public class FileUploadController {

    @Autowired
    FileUploadService fileUploadService;

    /**
     * 分布式系统
     * API:接口 发请求
     * SDK:工具包 导包
     * @param file
     * @return
     */
    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestPart("file")MultipartFile file) throws Exception {

        //1，获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //2,获取当前文件上传的大小
        long size = file.getSize();

        //TODO 保存到Minio,并返回访问路径
        String url = fileUploadService.uploadToMinio(file);
//        byte[] bytes = file.getBytes();
//
//        InputStream inputStream = file.getInputStream();
        return Result.ok(url);
    }

}
